package com.briup.es.web.controller;

import com.briup.es.config.response.Result;
import com.briup.es.domain.vo.ShopCarVO;
import com.briup.es.service.ShopCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@Api(tags = "购物车模块")
@Validated
@RestController
@RequestMapping("/shopCar")
public class ShopCarController {

    @Resource
    private ShopCarService service;

    @ApiOperation("查询当前用户的购物车列表")
    @GetMapping
    public Result<?> queryAll() {
        List<ShopCarVO> voList = service.queryAll();
        return Result.success(voList)
                .apply("获取成功");
    }

    @ApiOperation("添加图书到购物车")
    @PutMapping("/{id}")
    public Result<?> addShopCar(@PathVariable Integer id) {
        service.addShopCar(id);
        return Result.success();
    }

    // id是购物车项的id，num为购物车项中指定图书的数量
    @ApiOperation("修改购物车中商品的数量")
    @PutMapping("/{id}/{num}")
    public Result<?> updateNum(@PathVariable Integer id, @PathVariable Integer num) {
        service.updateNumById(id, num);
        return Result.success();
    }

    @ApiOperation("删除指定的购物车项")
    @DeleteMapping
    public Result<?> deleteShopCar(
            @NotEmpty(message = "购物车id不能为空")
            @RequestBody List<Integer> ids) {
        service.deleteBatch(ids);
        return Result.success();
    }
}
